﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEngine;

public class USAnimationCurveEditor
{
    private bool autoSmoothTangents = true;
    private Color boldGrid = new Color(0.4f, 0.4f, 0.4f, 0.5f);
    private Color[] curveColours = new Color[] { new Color(1f, 0f, 0f, 1f), new Color(0f, 1f, 0f, 1f), new Color(0f, 0f, 1f, 1f), new Color(1f, 1f, 0f, 1f) };
    private bool displayingCurve0 = true;
    private bool displayingCurve1 = true;
    private bool displayingCurve2 = true;
    private bool displayingCurve3 = true;
    private float duration = 10f;
    private USPropertyInfo edittingProperty;
    private Texture keyframeTexture;
    private Color labelColour = new Color(0f, 0f, 0f, 0.4f);
    private Color lightGrid = new Color(0.4f, 0.4f, 0.4f, 0.1f);
    private int MaxDisplayY;
    private float maxX;
    private float maxY;
    private float minX;
    private float minY;
    private USInternalCurve selectedCurve;
    private int selectedTangent = -1;
    private float tangentLength = 30f;
    private float titleBarHeight = 20f;
    private Color zeroGridV = new Color(0f, 1f, 0f, 0.4f);

    public void AddKeyframe(float time)
    {
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            Undo.RegisterUndo(curve, "Add Keyframe");
            float num = curve.Evaluate(time);
            curve.AddKeyframe(time, num);
        }
    }

    public void AddRightClickOptions(GenericMenu contextMenu, Vector2 mousePosition)
    {
        contextMenu.AddSeparator(string.Empty);
        USInternalKeyframe keyframe = null;
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            foreach (USInternalKeyframe keyframe2 in curve.Keys)
            {
                if (USSelection.IsInUnitySelection(keyframe2))
                {
                    keyframe = keyframe2;
                }
            }
        }
        if (keyframe == null)
        {
            ContextData userData = new ContextData();
            userData.time = USControl.convertEventPanePositionToTime(mousePosition.x);
            contextMenu.AddItem(new GUIContent("Add Keyframe"), false, new GenericMenu.MenuFunction2(this.ContextClickAddKey), userData);
        }
        else
        {
            bool brokenTangents = keyframe.BrokenTangents;
            ContextData data2 = new ContextData();
            data2.keyframe = keyframe;
            contextMenu.AddItem(new GUIContent("Broken"), brokenTangents, new GenericMenu.MenuFunction2(this.ContextClickBreakKey), data2);
            contextMenu.AddItem(new GUIContent("Smooth"), false, new GenericMenu.MenuFunction2(this.ContextClickSmoothKey), null);
            contextMenu.AddItem(new GUIContent("Flatten"), false, new GenericMenu.MenuFunction2(this.ContextClickFlatKey), null);
            contextMenu.AddItem(new GUIContent("Right Tangent/Linear"), false, new GenericMenu.MenuFunction2(this.ContextClickRightTangentLinear), null);
            contextMenu.AddItem(new GUIContent("Right Tangent/Constant"), false, new GenericMenu.MenuFunction2(this.ContextClickRightTangentConstant), null);
            contextMenu.AddItem(new GUIContent("Left Tangent/Linear"), false, new GenericMenu.MenuFunction2(this.ContextClickLeftTangentLinear), null);
            contextMenu.AddItem(new GUIContent("Left Tangent/Constant"), false, new GenericMenu.MenuFunction2(this.ContextClickLeftTangentConstant), null);
        }
    }

    private void CalculateBounds()
    {
        this.maxX = this.maxY = float.NegativeInfinity;
        this.minX = this.minY = float.PositiveInfinity;
        int num = 0;
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            switch (num)
            {
                case 0:
                {
                    if (this.displayingCurve0)
                    {
                        break;
                    }
                    num++;
                    continue;
                }
                case 1:
                {
                    if (this.displayingCurve1)
                    {
                        break;
                    }
                    num++;
                    continue;
                }
                case 2:
                {
                    if (this.displayingCurve2)
                    {
                        break;
                    }
                    num++;
                    continue;
                }
                case 3:
                {
                    if (this.displayingCurve3)
                    {
                        break;
                    }
                    num++;
                    continue;
                }
            }
            foreach (USInternalKeyframe keyframe in curve.Keys)
            {
                this.minX = Mathf.Min(this.minX, keyframe.Time);
                this.maxX = Mathf.Max(this.maxX, keyframe.Time);
                this.minY = Mathf.Min(this.minY, keyframe.Value);
                this.maxY = Mathf.Max(this.maxY, keyframe.Value);
            }
            num++;
        }
    }

    private void ContextClickAddKey(object obj)
    {
        ContextData data = obj as ContextData;
        this.AddKeyframe(data.time);
    }

    private void ContextClickBreakKey(object obj)
    {
        ContextData data = obj as ContextData;
        USInternalKeyframe objectToUndo = data.keyframe;
        if (objectToUndo != null)
        {
            Undo.RegisterUndo(objectToUndo, "Break Keyframe Tangents");
            objectToUndo.BrokenTangents = !objectToUndo.BrokenTangents;
        }
    }

    private void ContextClickFlatKey(object obj)
    {
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            for (int i = 0; i < curve.Keys.Length; i++)
            {
                USInternalKeyframe selectedObject = curve.Keys[i];
                if (USSelection.IsInUnitySelection(selectedObject))
                {
                    Undo.RegisterUndo(selectedObject, "Flatten Key");
                    Undo.RegisterUndo(curve, "Flatten Key");
                    selectedObject.Flatten();
                }
            }
        }
    }

    private void ContextClickLeftTangentConstant(object obj)
    {
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            for (int i = 0; i < curve.Keys.Length; i++)
            {
                USInternalKeyframe selectedObject = curve.Keys[i];
                if (USSelection.IsInUnitySelection(selectedObject))
                {
                    Undo.RegisterUndo(selectedObject, "Left Tangent Constant");
                    Undo.RegisterUndo(curve, "Left Tangent Constant");
                    selectedObject.LeftTangentConstant();
                }
            }
        }
    }

    private void ContextClickLeftTangentLinear(object obj)
    {
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            for (int i = 0; i < curve.Keys.Length; i++)
            {
                USInternalKeyframe selectedObject = curve.Keys[i];
                if (USSelection.IsInUnitySelection(selectedObject))
                {
                    Undo.RegisterUndo(selectedObject, "Left Tangent Constant");
                    Undo.RegisterUndo(curve, "Left Tangent Constant");
                    selectedObject.LeftTangentLinear();
                }
            }
        }
    }

    private void ContextClickRightTangentConstant(object obj)
    {
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            for (int i = 0; i < curve.Keys.Length; i++)
            {
                USInternalKeyframe selectedObject = curve.Keys[i];
                if (USSelection.IsInUnitySelection(selectedObject))
                {
                    Undo.RegisterUndo(selectedObject, "Right Tangent Constant");
                    Undo.RegisterUndo(curve, "Right Tangent Constant");
                    selectedObject.RightTangentConstant();
                }
            }
        }
    }

    private void ContextClickRightTangentLinear(object obj)
    {
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            for (int i = 0; i < curve.Keys.Length; i++)
            {
                USInternalKeyframe selectedObject = curve.Keys[i];
                if (USSelection.IsInUnitySelection(selectedObject))
                {
                    Undo.RegisterUndo(selectedObject, "Right Tangent Linear");
                    Undo.RegisterUndo(curve, "Right Tangent Linear");
                    selectedObject.RightTangentLinear();
                }
            }
        }
    }

    private void ContextClickSmoothKey(object obj)
    {
        foreach (USInternalCurve curve in this.edittingProperty.curves)
        {
            for (int i = 0; i < curve.Keys.Length; i++)
            {
                USInternalKeyframe selectedObject = curve.Keys[i];
                if (USSelection.IsInUnitySelection(selectedObject))
                {
                    Undo.RegisterUndo(selectedObject, "Smooth Keyframe");
                    Undo.RegisterUndo(curve, "Smooth Keyframe");
                    selectedObject.Smooth();
                }
            }
        }
    }

    public float convertValueToPanePosition(float value, Rect myArea)
    {
        float num = (myArea.height - this.titleBarHeight) * 0.5f;
        float num2 = myArea.y + this.titleBarHeight;
        float num3 = num2 + num;
        float num4 = 0f;
        if (this.MaxDisplayY > 0)
        {
            num4 = value / ((float) this.MaxDisplayY);
        }
        return (num3 - (num4 * num));
    }

    private void DrawCurve(Rect pixelArea, USInternalCurve curve, Color curveColour)
    {
        if (curve.Keys.Length != 0)
        {
            Color color = GUI.color;
            Handles.color = curveColour;
            USInternalKeyframe o = curve.Keys[0];
            foreach (USInternalKeyframe keyframe2 in curve.Keys)
            {
                if (!keyframe2.Equals(o))
                {
                    int num2 = 40;
                    float num3 = keyframe2.Time - o.Time;
                    float num4 = num3 / ((float) num2);
                    float time = o.Time;
                    for (int j = 0; j <= num2; j++)
                    {
                        if (j != 0)
                        {
                            float num7 = time + (j * num4);
                            float num8 = time + ((j - 1) * num4);
                            float num9 = curve.Evaluate(num7);
                            float num10 = curve.Evaluate(num8);
                            Vector2 from = new Vector2(pixelArea.width * (num8 / this.duration), pixelArea.height * ((num10 / ((float) this.MaxDisplayY)) / 2f));
                            Vector2 to = new Vector2(pixelArea.width * (num7 / this.duration), pixelArea.height * ((num9 / ((float) this.MaxDisplayY)) / 2f));
                            from.y += pixelArea.height * 0.5f;
                            to.y += pixelArea.height * 0.5f;
                            from.y = pixelArea.height - from.y;
                            to.y = pixelArea.height - to.y;
                            this.DrawLine(from, to);
                        }
                    }
                    o = keyframe2;
                }
            }
            for (int i = 0; i < curve.Keys.Length; i++)
            {
                USInternalKeyframe selectedObject = curve.Keys[i];
                Vector2 vector3 = new Vector2(pixelArea.width * (selectedObject.Time / this.duration), pixelArea.height * ((selectedObject.Value / ((float) this.MaxDisplayY)) / 2f));
                vector3.y += pixelArea.height * 0.5f;
                vector3.y = pixelArea.height - vector3.y;
                bool flag = USSelection.IsInUnitySelection(selectedObject);
                GUI.color = Color.white;
                if (flag)
                {
                    GUI.color = Color.yellow;
                }
                GUI.Label(new Rect(vector3.x - 6f, vector3.y - 6f, 16f, 16f), this.keyframeTexture);
                if ((USSelection.GetNumOfSelections() == 1) && USSelection.IsInUnitySelection(selectedObject))
                {
                    Vector2 vector4 = new Vector2(vector3.x, vector3.y);
                    if (i < (curve.Keys.Length - 1))
                    {
                        USInternalKeyframe keyframe4 = curve.Keys[i + 1];
                        float x = USControl.convertTimeToEventPanePosition(keyframe4.Time);
                        float y = this.convertValueToPanePosition(keyframe4.Value, pixelArea) - pixelArea.y;
                        Vector2 vector5 = new Vector2(x, y);
                        ((Vector3) (vector5 - vector4)).Normalize();
                        float z = -57.29578f * Mathf.Atan(selectedObject.OutTangent);
                        Vector2 vector7 = (Vector2) (Quaternion.Euler(0f, 0f, z) * Vector2.right);
                        Vector2 vector8 = vector4 + ((Vector2) (vector7 * this.tangentLength));
                        Handles.color = new Color(this.boldGrid.r, this.boldGrid.g, this.boldGrid.b, this.boldGrid.a + 0.4f);
                        this.DrawLine(vector4, vector8);
                        GUI.color = Color.white;
                        if ((curve == this.selectedCurve) && (this.selectedTangent == 1))
                        {
                            GUI.color = Color.yellow;
                        }
                        GUI.Label(new Rect(vector8.x - 5f, vector8.y - 4f, 10f, 10f), this.keyframeTexture);
                    }
                    if (i > 0)
                    {
                        float num15 = -57.29578f * Mathf.Atan(selectedObject.InTangent);
                        Vector2 vector9 = (Vector2) (Quaternion.Euler(0f, 0f, num15) * Vector2.right);
                        Vector2 vector10 = vector4 + ((Vector2) (-vector9 * this.tangentLength));
                        Handles.color = new Color(this.boldGrid.r, this.boldGrid.g, this.boldGrid.b, this.boldGrid.a + 0.4f);
                        this.DrawLine(vector4, vector10);
                        GUI.color = Color.white;
                        if ((curve == this.selectedCurve) && (this.selectedTangent == 0))
                        {
                            GUI.color = Color.yellow;
                        }
                        GUI.Label(new Rect(vector10.x - 5f, vector10.y - 4f, 10f, 10f), this.keyframeTexture);
                    }
                }
            }
            GUI.color = color;
            Handles.color = color;
        }
    }

    private void DrawGrid(Rect pixelArea)
    {
        float currentXMarkerDist = USControl.zoomInfo.currentXMarkerDist;
        int num2 = Mathf.FloorToInt((pixelArea.width / currentXMarkerDist) + 0.5f);
        int num3 = 5;
        int num4 = (num3 * 2) + 1;
        int num5 = Mathf.CeilToInt(Mathf.Max(Mathf.Abs(this.maxY), Mathf.Abs(this.minY)) + 0.5f);
        this.MaxDisplayY = num5;
        num5 *= 2;
        int maxDisplayY = this.MaxDisplayY;
        float num7 = pixelArea.height / ((float) (num4 - 1));
        int num8 = 5;
        Color color = GUI.color;
        GUI.color = this.labelColour;
        for (int i = 0; i < num2; i++)
        {
            Vector2 from = new Vector2(i * currentXMarkerDist, pixelArea.yMin);
            Vector2 to = new Vector2(from.x, pixelArea.yMax);
            Handles.color = this.lightGrid;
            if ((i % num8) == 0)
            {
                if (i == 0)
                {
                    Handles.color = this.zeroGridV;
                }
                else
                {
                    Handles.color = this.boldGrid;
                }
            }
            this.DrawLine(from, to);
        }
        int num10 = -1;
        for (int j = -num3; j <= num3; j++)
        {
            Vector2 vector3 = new Vector2(pixelArea.xMin, (j + num3) * num7);
            Vector2 vector4 = new Vector2(pixelArea.xMax, vector3.y);
            vector3.y = pixelArea.height - vector3.y;
            vector4.y = vector3.y;
            Handles.color = this.lightGrid;
            if ((j % num8) == 0)
            {
                GUIStyle style = null;
                GUISkin uSeqSkin = USEditorUtility.USeqSkin;
                if (uSeqSkin != null)
                {
                    style = uSeqSkin.GetStyle("AnimCurveLabel");
                }
                GUI.color = color;
                GUI.Label(new Rect(vector3.x, (j <= 0) ? (vector3.y - 15f) : vector3.y, 50f, 20f), string.Format("{0:F}", num10 * maxDisplayY), style);
                GUI.color = this.labelColour;
                Handles.color = this.boldGrid;
                num10++;
            }
            this.DrawLine(vector3, vector4);
        }
        GUI.color = color;
    }

    private void DrawLine(Vector2 from, Vector2 to)
    {
        Handles.DrawLine((Vector3) from, (Vector3) to);
    }

    private void GetNearestTangent(Vector2 position, Rect myArea, out int nearestTangent, out USInternalCurve nearestCurve)
    {
        nearestTangent = -1;
        nearestCurve = null;
        if (USSelection.GetNumOfSelections() == 1)
        {
            float num = 6f;
            foreach (USInternalCurve curve in this.edittingProperty.curves)
            {
                foreach (USInternalKeyframe keyframe in curve.Keys)
                {
                    if (USSelection.IsInUnitySelection(keyframe))
                    {
                        float x = USControl.convertTimeToEventPanePosition(keyframe.Time);
                        float y = this.convertValueToPanePosition(keyframe.Value, myArea) - myArea.y;
                        float z = -57.29578f * Mathf.Atan(keyframe.OutTangent);
                        Vector2 vector = (Vector2) (Quaternion.Euler(0f, 0f, z) * Vector2.right);
                        float num6 = -57.29578f * Mathf.Atan(keyframe.InTangent);
                        Vector2 vector2 = (Vector2) (Quaternion.Euler(0f, 0f, num6) * Vector2.right);
                        Vector2 vector3 = new Vector2(x, y);
                        Vector2 a = vector3 + ((Vector2) (-vector2 * this.tangentLength));
                        Vector2 vector5 = vector3 + ((Vector2) (vector * this.tangentLength));
                        if (Vector2.Distance(a, position) < num)
                        {
                            nearestTangent = 0;
                            nearestCurve = curve;
                            return;
                        }
                        if (Vector2.Distance(vector5, position) < num)
                        {
                            nearestTangent = 1;
                            nearestCurve = curve;
                            return;
                        }
                    }
                }
            }
        }
    }

    public void GetSelectableObjectUnderPos(ref List<UnityEngine.Object> selectedObjects, Rect selectionArea, Rect myArea)
    {
        if (this.edittingProperty != null)
        {
            for (int i = 0; i < this.edittingProperty.curves.Count; i++)
            {
                USInternalCurve curve = this.edittingProperty.curves[i];
                switch (i)
                {
                    case 0:
                        if (this.displayingCurve0)
                        {
                            break;
                        }
                        goto Label_011C;

                    case 1:
                        if (this.displayingCurve1)
                        {
                            break;
                        }
                        goto Label_011C;

                    case 2:
                        if (this.displayingCurve2)
                        {
                            break;
                        }
                        goto Label_011C;

                    case 3:
                        if (this.displayingCurve3)
                        {
                            break;
                        }
                        goto Label_011C;
                }
                int num2 = 0;
                foreach (USInternalKeyframe keyframe in curve.Keys)
                {
                    float num4 = USControl.convertTimeToEventPanePosition(keyframe.Time);
                    float num5 = this.convertValueToPanePosition(keyframe.Value, myArea);
                    Rect rectA = new Rect(num4 - 5f, num5 - 5f, 10f, 10f);
                    if (USEditorUtility.DoRectsOverlap(rectA, selectionArea))
                    {
                        selectedObjects.Add(keyframe);
                    }
                    num2++;
                }
            Label_011C:;
            }
        }
    }

    public void OnGUI(Rect area, Rect myArea)
    {
        string path = "DarkKeyframe";
        if (this.keyframeTexture == null)
        {
            this.keyframeTexture = Resources.Load(path, typeof(Texture)) as Texture;
            if (this.keyframeTexture == null)
            {
                Debug.LogError("Couldn't find texture : " + path + " for USAnimationCurveEditor::ONGUI, it is possible it has been moved from the resources folder");
                return;
            }
        }
        if (this.edittingProperty.curves.Count < 1)
        {
            GUILayout.Label("No Curves To Edit", new GUILayoutOption[0]);
        }
        else
        {
            this.CalculateBounds();
            float width = (this.edittingProperty.curves.Count * 25f) + 135f;
            Rect screenRect = new Rect(area.width - width, 0f, width, area.height);
            GUILayout.BeginArea(screenRect);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(135f) };
            if (GUILayout.Button(!this.autoSmoothTangents ? "No Auto Smooth" : "Auto Smooth Tangents", options))
            {
                this.autoSmoothTangents = !this.autoSmoothTangents;
            }
            Color color = GUI.color;
            if (this.displayingCurve0)
            {
                GUI.color = Color.red;
            }
            if (GUILayout.Button("R", new GUILayoutOption[0]))
            {
                this.displayingCurve0 = !this.displayingCurve0;
            }
            GUI.color = color;
            if (this.edittingProperty.curves.Count > 1)
            {
                if (this.displayingCurve1)
                {
                    GUI.color = Color.red;
                }
                if (GUILayout.Button("G", new GUILayoutOption[0]))
                {
                    this.displayingCurve1 = !this.displayingCurve1;
                }
                GUI.color = color;
            }
            if (this.edittingProperty.curves.Count > 2)
            {
                if (this.displayingCurve2)
                {
                    GUI.color = Color.red;
                }
                if (GUILayout.Button("B", new GUILayoutOption[0]))
                {
                    this.displayingCurve2 = !this.displayingCurve2;
                }
                GUI.color = color;
            }
            if (this.edittingProperty.curves.Count > 3)
            {
                if (this.displayingCurve3)
                {
                    GUI.color = Color.red;
                }
                if (GUILayout.Button("Y", new GUILayoutOption[0]))
                {
                    this.displayingCurve3 = !this.displayingCurve3;
                }
                GUI.color = color;
            }
            GUILayout.EndHorizontal();
            GUILayout.EndArea();
            area.y += this.titleBarHeight;
            area.height -= this.titleBarHeight;
            GUILayout.BeginArea(area);
            area.x = 0f;
            area.y = 0f;
            this.DrawGrid(area);
            for (int i = 0; i < this.edittingProperty.curves.Count; i++)
            {
                switch (i)
                {
                    case 0:
                    {
                        if (this.displayingCurve0)
                        {
                            break;
                        }
                        continue;
                    }
                    case 1:
                    {
                        if (this.displayingCurve1)
                        {
                            break;
                        }
                        continue;
                    }
                    case 2:
                    {
                        if (this.displayingCurve2)
                        {
                            break;
                        }
                        continue;
                    }
                    case 3:
                    {
                        if (this.displayingCurve3)
                        {
                            break;
                        }
                        continue;
                    }
                }
                this.DrawCurve(area, this.edittingProperty.curves[i], this.curveColours[i]);
            }
            GUILayout.EndArea();
            int nearestTangent = -1;
            USInternalCurve nearestCurve = null;
            this.GetNearestTangent(Event.current.mousePosition, myArea, out nearestTangent, out nearestCurve);
            if (((nearestCurve != null) && (nearestTangent != -1)) && (Event.current.type == EventType.MouseDown))
            {
                this.selectedCurve = nearestCurve;
                this.selectedTangent = nearestTangent;
                Event.current.Use();
            }
            if ((Event.current.type == EventType.MouseUp) || (USSelection.GetNumOfSelections() != 1))
            {
                this.selectedCurve = null;
                this.selectedTangent = -1;
            }
            this.UpdateDragging(myArea);
        }
    }

    public void ProcessRemoveObject()
    {
        if (this.edittingProperty != null)
        {
            for (int i = 0; i < this.edittingProperty.curves.Count; i++)
            {
                USInternalCurve objectToUndo = this.edittingProperty.curves[i];
                foreach (USInternalKeyframe keyframe in objectToUndo.Keys)
                {
                    if (USSelection.IsInUnitySelection(keyframe))
                    {
                        Undo.RegisterUndo(keyframe, "Remove From Sequence");
                        Undo.RegisterUndo(objectToUndo, "Remove From Sequence");
                        objectToUndo.RemoveKeyframe(keyframe);
                    }
                }
            }
        }
    }

    public void SetEdittingProperty(USPropertyInfo property)
    {
        this.edittingProperty = property;
        if (this.edittingProperty != null)
        {
            if (this.edittingProperty.curves.Count > 0)
            {
                this.displayingCurve0 = true;
            }
            if (this.edittingProperty.curves.Count > 1)
            {
                this.displayingCurve1 = true;
            }
            if (this.edittingProperty.curves.Count > 2)
            {
                this.displayingCurve2 = true;
            }
            if (this.edittingProperty.curves.Count > 3)
            {
                this.displayingCurve3 = true;
            }
        }
    }

    private void UpdateDragging(Rect myArea)
    {
        if ((USSelection.GetNumOfSelections() == 1) && (Selection.objects.Length != 0))
        {
            object obj2 = Selection.objects[0];
            USInternalKeyframe keyframe = obj2 as USInternalKeyframe;
            if (((this.selectedTangent != -1) && (this.selectedCurve != null)) && (keyframe != null))
            {
                float x = USControl.convertTimeToEventPanePosition(keyframe.Time);
                float y = this.convertValueToPanePosition(keyframe.Value, myArea) - myArea.y;
                Vector2 vector = new Vector2(x, y);
                Vector2 to = Event.current.mousePosition - vector;
                to.Normalize();
                bool flag = to.y > 0f;
                float f = Vector2.Angle(Vector2.right, to);
                if ((this.selectedTangent == 1) && (f >= 90f))
                {
                    f = 90f;
                }
                if ((this.selectedTangent == 0) && (f <= 90f))
                {
                    f = 90f;
                }
                f = 0.01745329f * f;
                f = Mathf.Tan(f);
                if (flag)
                {
                    f = -1f * f;
                }
                if ((f > 10000f) || (f < -10000f))
                {
                    f = float.PositiveInfinity;
                }
                if (!keyframe.BrokenTangents)
                {
                    if (this.selectedTangent == 0)
                    {
                        keyframe.InTangent = f;
                        keyframe.OutTangent = f;
                    }
                    if (this.selectedTangent == 1)
                    {
                        keyframe.OutTangent = f;
                        keyframe.InTangent = f;
                    }
                }
                else
                {
                    if (this.selectedTangent == 0)
                    {
                        keyframe.InTangent = f;
                    }
                    if (this.selectedTangent == 1)
                    {
                        keyframe.OutTangent = f;
                    }
                }
                this.selectedCurve.BuildAnimationCurveFromInternalCurve();
            }
        }
    }

    public bool AutoSmoothTangents
    {
        get
        {
            return this.autoSmoothTangents;
        }
        set
        {
            this.autoSmoothTangents = value;
        }
    }

    public float Duration
    {
        set
        {
            this.duration = value;
        }
    }

    private class ContextData
    {
        public AnimationCurve curve;
        public USInternalKeyframe keyframe;
        public float time;
    }
}

